package org.flexlancer.databases
{
	public class VehicleTree
	{
		private static const TABLE:String  = "vehicletree";
		
		public static function List(condition:String = ""):Array{
			try{
				return DatabaseManager.Manager.Select(TABLE,null,condition);
			}catch(err:Error){
				throw err;
			}
			return new Array();
		}
		
		public static function Add(parmName:String, parmParentID:String):void{
			try{
				var insert:Object = new Object();
				insert.name = " '"+parmName+"' ";
				insert.parentid = parmParentID;
				DatabaseManager.Manager.Insert(TABLE,insert);
			}catch(err:Error){
				throw err;
			}
		}
		
		public static function Update(parmID:String,parmName:String,parmParentID:String):void{
			try{
				var update:Object = new Object();
				update.name = " '"+parmName+"' ";
				update.parentid = parmParentID;
				DatabaseManager.Manager.Update(TABLE,update,"id="+parmID);
			}catch(err:Error){
				throw err;
			}
		}
		
		public static function Delete(condition:String = ""):void{
			try{
				DatabaseManager.Manager.Delete(TABLE,condition);
			}catch(err:Error){
				throw err;
			}
		}
		
		public static function Tree():XML{
			try{
				return new XML("<node>"+BuildTree("0").toXMLString()+"</node>");
			}catch(err:Error){
				throw err;
			}
			return null;
		}
		
		public static function Init():void{
			try{
				var tmp:XML = <Cars label="CARS">
			<Car label="TOYOTA">
				<Car label="COROLLA">
					<Car label="XLI">
						<Year label="1999" />
						<Year label="2000" />
						<Year label="2001" />
						<Year label="2002" />
						<Year label="2003" />
						<Year label="2004" />
						<Year label="2005" />
						<Year label="2006" />
						<Year label="2007" />
						<Year label="2008" />
						<Year label="2009" />
						<Year label="2010" />
					</Car>
					<Car label="GLI">
						<Year label="1999" />
						<Year label="2000" />
						<Year label="2001" />
						<Year label="2002" />
						<Year label="2003" />
						<Year label="2004" />
						<Year label="2005" />
						<Year label="2006" />
						<Year label="2007" />
						<Year label="2008" />
						<Year label="2009" />
						<Year label="2010" />
					</Car>
				</Car>
			</Car>
			<Car label="HONDA">
				<Car label="CIVIC">
					<Year label="1999" />
					<Year label="2000" />
					<Year label="2001" />
					<Year label="2002" />
					<Year label="2003" />
					<Year label="2004" />
					<Year label="2005" />
					<Year label="2006" />
					<Year label="2007" />
					<Year label="2008" />
					<Year label="2009" />
					<Year label="2010" />
				</Car>
				<Car label="CITY">
					<Year label="1999" />
					<Year label="2000" />
					<Year label="2001" />
					<Year label="2002" />
					<Year label="2003" />
					<Year label="2004" />
					<Year label="2005" />
					<Year label="2006" />
					<Year label="2007" />
					<Year label="2008" />
					<Year label="2009" />
					<Year label="2010" />
				</Car>
			</Car>
		</Cars>;
				for each ( var singleCar:XML in tmp.children() ){
					VehicleTree.Add(String(singleCar.@label),"0");
					var sid:String = DatabaseManager.Manager.LastInsertRowID.toString();
					
					for each ( var singleModel:XML in singleCar.children() ){
						VehicleTree.Add(String(singleModel.@label),sid);
						var smid:String = DatabaseManager.Manager.LastInsertRowID.toString();
						
						for each ( var singleSubModel:XML in singleModel.children() ) {
							VehicleTree.Add(String(singleSubModel.@label),smid);
							var ssmid:String = DatabaseManager.Manager.LastInsertRowID.toString();

							for each ( var makeYear:XML in singleSubModel.children() ) {
								VehicleTree.Add(String(makeYear.@label),ssmid);
							}
						}
					}
				}
			}catch(err:Error){
				throw err;
			}
		}
			
		private static function BuildTree(parmID:String):XMLList{
			try{
				var arrList:Array = List("parentid="+parmID);
				var xmlReturn:XML = new XML(<node />);
				
				for each ( var singleObj:Object in arrList ){
					var insertNode:XML = new XML("<node />");
					insertNode.@id = singleObj.id;
					insertNode.@name = singleObj.name;
					insertNode.@parentid = singleObj.paretnid;
					
					insertNode.appendChild(BuildTree(singleObj.id));
					xmlReturn.appendChild(insertNode);
				}
				return xmlReturn.children();
			}catch(err:Error){
				throw err;
			}
			return null;
		}
	}
}